home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / lang_ext / vbawk / vb-awk.bas < prev    next >
Encoding:
BASIC Source File  |  1994-08-15  |  8.7 KB  |  333 lines

  1. Global FNR As Long       'File Number of Records
  2. Global NR  As Long       'Number of Records
  3. Global NF As Integer     'Number of Files
  4. Global FILENAME As String   'Name of current file
  5. Global STATUSBOX As Integer 'Flag; if TRUE, indicates the app wishes to activate the built-in statusbox
  6. Global STATUSFILE As Integer 'Flag; if TRUE, indicates that WriteToStatus will write to a file called VB-AWK.LOG
  7. Global STATUSMESSAGES As Integer  'flag; if TRUE, indicates the app wishes to activate the built-in status messages "Processing...finished"
  8. Global APPNAME As String    'Name of application
  9. Global BINARYMODE As Integer    'Flag indicating binary file mode
  10. Global RECLEN As Long           'holds record length for binary reads
  11. Global COMPAREMODE As Integer   'holds global comparison mode, 0 = case-sensitive, 1 = case-insensitive
  12. Global ABORT As Integer         'indicates stop processing right now!
  13.  
  14. 'Constants:
  15. Global Const COMPARE_CASESENSITIVE = 0
  16. Global Const COMPARE_CASEINSENSITIVE = 1
  17.  
  18. 'Locals:
  19. Dim VerNum As String        'VB-AWK version number
  20.  
  21. Sub AppendToStatus (AppendStr$)
  22.     If STATUSBOX Then
  23.         Ndx% = Status.List1.ListCount - 1
  24.         Status.List1.List(Ndx%) = Status.List1.List(Ndx%) & AppendStr$
  25.         DoEvents
  26.     End If
  27.     If STATUSFILE Then
  28.         Seek #3, (Seek(3) - 2)   'seek prior to preceeding cr/lf
  29.         Print #3, AppendStr$
  30.     End If
  31. End Sub
  32.  
  33. Sub DoAFile (TheFile As String)
  34.     Dim TheLine As String
  35.  
  36.     If ABORT Then
  37.         Exit Sub
  38.     End If
  39.     If STATUSMESSAGES Then
  40.         WriteToStatus "Processing: " & TheFile & "..."
  41.     End If
  42.     NF = NF + 1
  43.     FILENAME = TheFile
  44.     FNR = 0
  45.  
  46.     'Call the user hook for pre-file processing
  47.     If Not BeforeAFile() Then
  48.         'User hook has specified skipping this file
  49.         GoTo Finished2
  50.     End If
  51.     On Error GoTo badopen
  52.     If BINARYMODE Then
  53.         Open TheFile For Binary As #1
  54.     Else
  55.         Open TheFile For Input As #1
  56.     End If
  57.     On Error GoTo BadTempOpen
  58.     TempFile$ = "\xyzzy.tmp"
  59.     If BINARYMODE Then
  60.         Open TempFile$ For Binary As #2
  61.     Else
  62.         Open TempFile$ For Output As #2
  63.     End If
  64.     
  65.     'do the file
  66.     On Error GoTo Finished
  67.     While 1
  68.         If BINARYMODE Then
  69.             TheLine = Input$(RECLEN, #1)
  70.             If TheLine = "" Then GoTo Finished
  71.         Else
  72.             Line Input #1, TheLine
  73.         End If
  74.         FNR = FNR + 1
  75.         NR = NR + 1
  76.         If DoALine(TheLine) Then
  77.             If BINARYMODE Then
  78.                 Put #2, , TheLine
  79.             Else
  80.                 Print #2, TheLine
  81.             End If
  82.         End If
  83.     Wend
  84.  
  85. badopen:
  86.     AppendToStatus "ERROR '" & Error$ & "' OPENING FILE "
  87.     Exit Sub
  88.  
  89. BadTempOpen:
  90.     AppendToStatus "ERROR '" & Error$ & "' OPENING TEMP FILE \XYZZY.TMP "
  91.     Close #1
  92.     Exit Sub
  93.  
  94. Finished:
  95.     Close #1
  96.     Close #2
  97.     FileCopy TempFile$, TheFile
  98.     Kill TempFile$
  99.     'fall through
  100.  
  101. Finished2:
  102.     If STATUSMESSAGES Then
  103.         AppendToStatus "Finished."
  104.     End If
  105.     AfterAFile
  106.     DoEvents
  107.     Exit Sub
  108.  
  109. End Sub
  110.  
  111. Sub DoDirectory (TheDir As String)
  112.     Dim Files() As String
  113.  
  114.     If Right$(TheDir, 1) = "\" Then
  115.         CorrectedDir$ = TheDir
  116.     Else
  117.         CorrectedDir$ = TheDir & "\"
  118.     End If
  119.     NumFiles% = ListAllFiles(Files(), CorrectedDir$ & MultiOpen.Pattern.Text)
  120.     For i% = 0 To (NumFiles% - 1)
  121.         If ABORT Then
  122.             Exit Sub
  123.         End If
  124.         FullFileName$ = CorrectedDir$ & Files(i%)
  125.         If GetAttr(FullFileName$) And ATTR_DIRECTORY Then
  126.             DoDirectory FullFileName$
  127.         Else
  128.             DoAFile FullFileName$
  129.         End If
  130.     Next i%
  131. End Sub
  132.  
  133. 'This function lists all the files and subdirectories in a directory,
  134. 'and returns them in the array FilesArray.  FilesArray is a variable-
  135. 'size array of strings which must be of size 0 upon entry to this
  136. 'routine.
  137. Function ListAllFiles (FilesArray() As String, Pattern As String) As Integer
  138.     Dim Count As Integer        'how many files processed so far
  139.     Dim ArraySize As Integer    'size of files array
  140.     Dim ArrayInc As Integer     'amount to increment array size
  141.  
  142.     ArrayInc = 5
  143.     CurFile$ = Dir$(Pattern, ATTR_DIRECTORY)
  144.     While CurFile$ <> ""
  145.         'Resize array if necessary.  We resize the array in increments
  146.         'of a size held in ArrayInc.
  147.         If Count >= ArraySize Then
  148.             ArraySize = ArraySize + ArrayInc
  149.             ReDim Preserve FilesArray(ArraySize - 1)
  150.         End If
  151.  
  152.         'Copy current file into array
  153.         If CurFile$ <> "." And CurFile$ <> ".." Then
  154.             FilesArray(Count) = CurFile$
  155.             Count = Count + 1
  156.         End If
  157.  
  158.         'one more time...
  159.         CurFile$ = Dir$
  160.     Wend
  161.     ListAllFiles = Count
  162. End Function
  163.  
  164. Sub Main ()
  165.     VerNum = "VB-AWK Version 1.0"
  166.     APPNAME = "VB-AWK"
  167.     STATUSBOX = True
  168.     STATUSMESSAGES = True
  169.     If Not BeforeAllFiles() Then  'call user preprocessing routine
  170.         End
  171.     End If
  172.     If Command$ = "" Then
  173.         'no command line, so show file-select dialog
  174.         MultiOpen.Show 1
  175.         If STATUSBOX Then
  176.             Status.Show
  177.         End If
  178.         If STATUSFILE Then
  179.             OpenLogFile
  180.         End If
  181.         If MultiOpen.ExpandDirectories.Value = 1 Then
  182.             GoSub RecurseDirectories
  183.         Else
  184.             GoSub DoFiles
  185.         End If
  186.     Else
  187.         'found command line, process it
  188.         If STATUSBOX Then
  189.             Status.Show
  190.         End If
  191.         If Left$(Command$, 1) = "@" Then
  192.             GoSub ProcessListFile
  193.         Else
  194.             GoSub ProcessFileSpec
  195.         End If
  196.     End If
  197.  
  198.     'finished; final cleanup
  199.     AfterAllFiles
  200.     If STATUSBOX Then
  201.         Status.StopBtn.Enabled = False
  202.     End If
  203.     If STATUSMESSAGES Then
  204.         If ABORT Then
  205.             WriteToStatus "Processing terminated by user."
  206.         Else
  207.             If NF <> 1 Then
  208.                 FileOrFiles$ = " files!"
  209.             Else
  210.                 FileOrFiles$ = " file!"
  211.             End If
  212.             WriteToStatus "Finished processing " & NF & FileOrFiles$
  213.         End If
  214.     End If
  215.     If STATUSFILE Then
  216.         Close #3
  217.     End If
  218.     Exit Sub
  219.  
  220. RecurseDirectories:
  221.     DoDirectory (MultiOpen.File1.Path)
  222.     Return
  223.  
  224. DoFiles:
  225.     If Mid$(MultiOpen.File1.Path, Len(MultiOpen.File1.Path), 1) <> "\" Then
  226.         CorrectedPath = MultiOpen.File1.Path & "\"
  227.     Else
  228.         CorrectedPath = MultiOpen.File1.Path
  229.     End If
  230.     For i% = 0 To MultiOpen.File1.ListCount - 1
  231.         If MultiOpen.File1.Selected(i%) Then
  232.             CurFile$ = CorrectedPath & MultiOpen.File1.List(i%)
  233.             DoAFile CurFile$
  234.         End If
  235.     Next i%
  236.     Return
  237.  
  238. ProcessListFile:
  239.     On Error GoTo BadListOpen
  240.     ListFileName$ = Mid$(Command$, 2)
  241.     Open ListFileName$ For Input As #10
  242.     On Error GoTo ListFinished
  243.     While True
  244.         Line Input #10, TheFile$
  245.         DoAFile TheFile$
  246.     Wend
  247.     Close #10
  248.     Return
  249.  
  250. ProcessFileSpec:
  251.     On Error GoTo BadSpec
  252.     SplitFilePath Command$, Drive$, Dirs$, Unused1$, Unused2$
  253.     TheFile$ = Dir$(Command$)
  254.     While TheFile$ <> ""
  255.         If Drive$ <> "" Then
  256.             Colon$ = ":"
  257.         Else
  258.             Colon$ = ""
  259.         End If
  260.         If Dirs$ <> "" Then
  261.             Slash$ = "\"
  262.         Else
  263.             Slash$ = ""
  264.         End If
  265.         DoAFile Drive$ & Colon$ & Dirs$ & Slash$ & TheFile$
  266.         TheFile$ = Dir$
  267.     Wend
  268.     Return
  269.  
  270. BadSpec:
  271.     MsgBox "Error on file spec: " & Error$
  272.     Exit Sub
  273.  
  274. BadListOpen:
  275.     MsgBox "Couldn't open list file " & Command$ & ", error: " & Error$
  276.     Exit Sub
  277.  
  278. ListFinished:
  279.     Close #10
  280.     Return
  281.  
  282. End Sub
  283.  
  284. Sub OpenLogFile ()
  285.     On Error GoTo badlogopen
  286.     Open "VB-AWK.LOG" For Output As #3
  287.     Exit Sub
  288.  
  289. badlogopen:
  290.     MsgBox "Error opening log file; will not use it."
  291.     STATUSFILE = False
  292.     Exit Sub
  293. End Sub
  294.  
  295. Sub SaveListBox (FilNam As String, LB As ListBox)
  296.     On Error GoTo badsaveopen
  297.     Open FilNam For Output As #5
  298.     On Error GoTo BadWrite
  299.     For i% = 0 To LB.ListCount - 1
  300.         Print #5, LB.List(i%)
  301.     Next i%
  302.     Close #5
  303.     Exit Sub
  304.  
  305. BadWrite:
  306.     MsgBox "Error: " & Error$ & " writing to file " & FilNam
  307.     Close #5
  308.     Exit Sub
  309.  
  310. badsaveopen:
  311.     MsgBox "Error: " & Error$ & " opening file " & FilNam
  312.     Exit Sub
  313. End Sub
  314.  
  315. Sub WriteToStatus (TheStr As String)
  316.     On Error GoTo memerr
  317.     If STATUSBOX Then
  318.         Status.List1.AddItem TheStr
  319.         Status.List1.ListIndex = Status.List1.ListCount - 1
  320.         DoEvents
  321.     End If
  322.     If STATUSFILE Then
  323.         Print #3, TheStr
  324.     End If
  325.     Exit Sub
  326.  
  327. memerr:
  328.     MsgBox "Status box out of memory; will clear it and continue."
  329.     Status.List1.Clear
  330.     Exit Sub
  331. End Sub
  332.  
  333.